# -*- coding: utf-8 -*-

'''eof
name:申请企业存量业务合同状态校验
code:R71_EntInventoryBusinessContractStatusCheck_ZC
tableName:
columnName:
groups:业务其他规则校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
from datetime import datetime

reload(sys)
sys.setdefaultencoding('utf-8')


def get_age(str1):
    try:
        date1 = datetime.now()
        date2 = date_format(str1)
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        age = diff_year + diff_month / 12.0 + diff_day / 365.0
        return age
    except:
        return u'缺失值'


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False


def getRule61():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    abnormalloan = json.loads(ratingRequest.get("abNormalloan"))
    # abnormalloan = ratingRequest.get("abNormalloan")
    resultList = []
    if abnormalloan not in null_type_list and isinstance(abnormalloan, list):
        if len(abnormalloan) >= 1:
            for i in abnormalloan:
                try:
                    customertype = i.get("customerType")
                    businessType = i["businessType"]
                except:
                    resultList.append(None)
                    continue
                if customertype not in null_type_list:
                    if customertype == "3":
                        if businessType == "10708C":
                            resultList.append(1)
                        elif businessType == "3010":
                            resultList.append(1)
                        else:
                            resultList.append(0)
                    else:
                        resultList.append(1)
                else:
                    resultList.append(None)
            if 0 in resultList:
                return 0
            elif 1 in resultList or len(resultList) < 1:
                return 1
            else:
                raise ValueError("网贷传输的数据字段缺失或不符合要求")
        else:
            return 1
    else:
        raise RuntimeError("网贷传输的数据格式错误或者为空")


if __name__ == "__main__":
    ratingRequest = {"legalPersonIdCard": "500101199202192411", "timeLimit": "12",
                     "abNormalloan": [{"balance": "30", "businessType": u"个人业务", "customerType": None},
                                      {"balance": "30", "businessType": u"个业务", "customerType": "2", "contratctNo": "123123", "vouchType": u"信用"},
                                      {"balance": "30", "businessType": u"10708C", "customerType": "3", "contratctNo": "123123", "vouchType": u"信用"},
                                      {"balance": "30", "businessType": u"个业务", "customerType": "1"}]}
    aa = getRule61()
    print aa

result = getRule61()
